package com.wx.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.wx.constant.MessageConstant;
import com.wx.entity.Result;
import com.wx.pojo.OrderSetting;

import com.wx.service.OrdersettingService;
import com.wx.utils.POIUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 预约设置相关的crud
 */
@RestController
@RequestMapping("/pages/ordersetting")
public class OrdersettingController {
    @Reference
    private OrdersettingService service;
    /**
     * 上传预约表格
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile){
            // 获得上传的文件，把文件解析对象
        try {
            List<String[]> list = POIUtils.readExcel(excelFile);
            List ordersettingList= new LinkedList<OrderSetting>();
            // 把数组中的数据全部转换一个对象
            for (String[] order : list) {
                OrderSetting orderSetting = new OrderSetting();
                Date parse = new SimpleDateFormat("yyyy/MM/dd").parse(order[0]);
                orderSetting.setOrderDate(parse);
                orderSetting.setNumber(Integer.parseInt(order[1]));
                // 添加到ordersettingList中
                ordersettingList.add(orderSetting);
            }
            // 调用远程方法
            service.batchAddOerdersetting(ordersettingList);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    /**
     * 查询出所有的预约信息
     * 一个月以内
     */
    @RequestMapping("/getAllOrder")
    public Result getAllOrder(String date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date1 = sdf.parse(date + "-01");
            Date date2 = sdf.parse(date + "-31");
            ArrayList<Date> list = new ArrayList<>();
            list.add(date2);
            list.add(date1);
            List<Map> mapList = service.findAllMessage(list);
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,mapList);

        } catch (ParseException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("/update")
    public Result update(@RequestBody OrderSetting orderSetting){

        try {
            service.updateOrderByDate(orderSetting);
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }
}
